#include "mainmusic.h"
#include "ui_mainmusic.h"
#include <QDebug>
#include <QFileDialog>
#include <QFileInfo>

mainMusic::mainMusic(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainMusic)
{
    ui->setupUi(this);
    ui->btn_play->setProperty("mode","pause");
    ui->pushButton->setProperty("mode","middle");
    ui->tabWidget->hide();
    ui->pushButton_2->hide();
    this->player = new QMediaPlayer();
    this->playlist = new QMediaPlaylist();
    player->setVolume(50);
    player->setPlaylist(playlist);
    userIsSeeking = false;
    ui->verticalSlider->hide();
    ui->verticalSlider->setValue(50);
    // 连接信号和槽
    connect(player, &QMediaPlayer::durationChanged, this, &mainMusic::onDurationChanged);
    connect(player, &QMediaPlayer::positionChanged, this, &mainMusic::onPositionChanged);
}

mainMusic::~mainMusic()
{
    delete ui;
}

void mainMusic::on_btn_return_clicked()
{
    this->parentWidget()->show();
    this->hide();
}

void mainMusic::on_btn_play_clicked()
{
    QString mode = ui->btn_play->property("mode").toString();
    qDebug() << mode;
    if(mode == "pause"){
        ui->btn_play->setProperty("mode","play");
        player->play();
    }else {
        ui->btn_play->setProperty("mode","pause");
        player->pause();
    }
    ui->btn_play->style()->unpolish(ui->btn_play);
    ui->btn_play->style()->polish(ui->btn_play);
    ui->btn_play->update();
}

void mainMusic::on_btn_openfile_clicked()
{
    QStringList fileNames = QFileDialog::getOpenFileNames(
        this,
        tr("Select Multiple Files"),
        "C:/Users/Administrator/Music",
        tr("Audio Files (*.mp3 *.ogg)")
    );
    for (QString file : fileNames) {
        this->playlist->addMedia(QMediaContent(QUrl::fromLocalFile(file)));
        QFileInfo fileInfo(file);
        ui->listWidget->addItem(fileInfo.fileName());
    }
    if(ui->listWidget->count()==fileNames.size()){
        ui->listWidget->setCurrentRow(0);
    }
}

void mainMusic::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    int row = ui->listWidget->currentRow();
    this->playlist->setCurrentIndex(row);
    this->player->play();
    ui->btn_play->setProperty("mode","play");
    ui->btn_play->style()->unpolish(ui->btn_play);
    ui->btn_play->style()->polish(ui->btn_play);
    ui->btn_play->update();
}

void mainMusic::on_btn_prev_clicked()
{
    playlist->previous();
    player->play();
}

void mainMusic::on_btn_next_clicked()
{
    playlist->next();
    player->play();
}

void mainMusic::onDurationChanged(qint64 duration)
{
    if (!userIsSeeking) {
        ui->horizontalSlider->setMaximum(duration);
    }
}

void mainMusic::onPositionChanged(qint64 position)
{
    if (!userIsSeeking) {
        ui->horizontalSlider->setValue(position);
    }
}

void mainMusic::on_horizontalSlider_sliderMoved(int position)
{
    userIsSeeking = true;
    player->setPosition(position);
    userIsSeeking = false;
}

void mainMusic::on_pushButton_clicked()
{
    if(!ui->verticalSlider->isVisible()){
        ui->verticalSlider->show();
    }else {
        ui->verticalSlider->hide();
}
}

void mainMusic::on_verticalSlider_sliderReleased()
{
    int value = ui->verticalSlider->value();
    player->setVolume(value);
    
    // 根据音量值设置按钮样式
    if(value == 0){
        ui->pushButton->setProperty("mode","min");
    }
    else if(value == 100){
        ui->pushButton->setProperty("mode","max");
    }
    else {
        ui->pushButton->setProperty("mode","middle");
    }
    
    // 强制更新按钮样式
    ui->pushButton->style()->unpolish(ui->pushButton);
    ui->pushButton->style()->polish(ui->pushButton);
    ui->pushButton->update();
}

void mainMusic::on_pushButton_3_clicked()
{
    //right
    ui->tabWidget->show();
    ui->pushButton_2->show();
    ui->pushButton_3->hide();
}

void mainMusic::on_pushButton_2_clicked()
{
    //left
    ui->tabWidget->hide();
    ui->pushButton_2->hide();
    ui->pushButton_3->show();
}
